package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.dto.OrderPageDTO;
import com.bigger.entity.AddressBook;
import com.bigger.entity.Employee;
import com.bigger.entity.Order;
import com.bigger.entity.OrderDetail;
import com.bigger.exception.OrderException;
import com.bigger.exception.PowerException;
import com.bigger.mapper.AddressBookMapper;
import com.bigger.mapper.EmployeeMapper;
import com.bigger.mapper.OrderDetailMapper;
import com.bigger.mapper.OrderMapper;
import com.bigger.result.PageResult;
import com.bigger.service.OrderService;
import com.bigger.vo.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public OrderDetailVO getById(Long id) {
        Order order = orderMapper.selectById(id);
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<OrderDetail>()
                .eq("order_id",id);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper);
        AddressBook addressBook = addressBookMapper.selectById(order.getAddressId());
        OrderDetailVO orderDetailVO=new OrderDetailVO(order.getOrderNumber(),order.getStatus(),order.getPrice(),orderDetails,addressBook);
        return orderDetailVO;
    }

    @Override
    public void takeOrder(Long id) {
        Order order = orderMapper.selectById(id);
        Employee employee = employeeMapper.selectById(BaseContext.getCurrentId());//获取自身信息
        if (!order.getStatus().equals(Order.WAIT_GIVE)){
            throw new OrderException(ErrorConstant.ORDER_STATUS_ERROR);
        }
        order.setStatus(Order.GIVING);//将订单转换为接单状态
        order.setEmployeeId(BaseContext.getCurrentId());//将接单人设置为自己
        order.setEmplName(employee.getName()); //将接单人姓名填为自己
        orderMapper.updateById(order);
    }

    @Override
    public void finishOrder(Long id) {
        Order order = orderMapper.selectById(id);
        if (!order.getStatus().equals(Order.GIVING)){
            throw new OrderException(ErrorConstant.ORDER_STATUS_ERROR);
        }
        if(!order.getEmployeeId().equals(BaseContext.getCurrentId())){
            throw new PowerException(ErrorConstant.INSUFFICIENT_PRIVILEGES);
        }
        order.setStatus(Order.FINISH);//将订单转换为完成状态
        order.setFinishTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        orderMapper.updateById(order);
    }

    @Override
    public PageResult<Order> page(OrderPageDTO orderPageDTO) {
        Page<Order> page=new Page<>(orderPageDTO.getPage(),orderPageDTO.getPageSize());
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>()
                .eq("status",orderPageDTO.getStatus())
                .eq("employee_id",BaseContext.getCurrentId());
        if (orderPageDTO.getStatus().equals(Order.WAIT_GIVE)){
            wrapper=new QueryWrapper<Order>()
                    .eq("status",orderPageDTO.getStatus());
        }
        Page<Order> orderPage = orderMapper.selectPage(page, wrapper);
        PageResult<Order> orderPageResult=new PageResult<>(orderPage.getTotal(),orderPage.getRecords());
        return orderPageResult;
    }
}
